package classes.view.component
{
	import classes.controller.DAOSendJS;
	import classes.controller.ESendJS;
	import classes.model.VOData;
	import classes.model.VOIsLocal;
	
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.text.TextField;
	import flash.text.TextFormat;

	public class GameNotice extends Sprite
	{
		private var _box:Array;
		private var _subject:NoticeSubjectObj;
		private var _no:TextField;
		private var _title:TextField;
		private var _secret:Sprite;
		
		public function GameNotice()
		{
			setLayOut();
			defaultSetting();
		}
		
		public function set notice_switch($switch:Boolean):void
		{
			if($switch){
				this.visible = true;
				init_data();
			}else{
				this.visible = false;
				remove_data();
			}
		}
		
		private function init_data():void
		{
			if(VOIsLocal.getLocalInstance().isLocal == "FILE"){
				//test.
				var loader:URLLoader = new URLLoader();
				loader.addEventListener( Event.COMPLETE,	onDataLoaded);
				try {
					loader.load( new URLRequest('sample/roominfo.json'));
				}catch ( error:Error ) {
					trace( "Unable to load requested document." );
				}
			}else{
				DAOSendJS.getLoadInstance().addEventListener(ESendJS.EVENT_ROOMINFO,	onRoomInfoLoadcomplete);	
			}
		}
		
		private function remove_data():void
		{
			
		}
		
		private function onDataLoaded(e:Event):void
		{
			var jsonParsed:*  	= JSON.parse(e.target.data);
			var obj:Object 		= jsonParsed.data[0];

			dataSetting 		= obj;
		}
		
		private function onRoomInfoLoadcomplete(e:ESendJS):void
		{
			var obj:Object 	= e._obj;
			dataSetting = obj;
		}
		
		private function set dataSetting($obj:Object):void
		{
			VOData.getDataInstance().roomID	= $obj.roomID;
			
			this._subject.subjectTitle 		= $obj.sb_name;
			this._no.text              		= 'No.'+String($obj.roomID);
			this._title.text 				= $obj.title;

			if($obj.secret == 1){
				_secret.visible = true;
			}else{
				_secret.visible = false;
			}

			if(_secret.visible == false){
				this._title.x = 100;
			}else{
				this._title.x = 124;
			}
		}
		
		private function setLayOut():void
		{
			if(_box==null)		_box 			= new Array();
			
			_box[0] = this.getChildByName('box_0')	as Sprite;
			_box[1] = this.getChildByName('box_1')	as Sprite;
			_box[2] = this.getChildByName('box_2')	as Sprite;
			
			this._subject	= this.getChildByName('subject_mc')		as NoticeSubjectObj;
			this._no		= this.getChildByName('no_txt')			as TextField;
			this._title		= this.getChildByName('title_txt')		as TextField;
			this._secret	= this.getChildByName('secret_mc')		as Sprite;
		}
		
		private function defaultSetting():void
		{
			this.visible = false;
			this._secret.visible = false;
			
			var textformat_letterspacing:TextFormat = new TextFormat();
			textformat_letterspacing.letterSpacing = -0.7;
			
			_no.setTextFormat(textformat_letterspacing);
			_title.setTextFormat(textformat_letterspacing);
		}
	}
}